/*
 @description: 后端管理员增删查改

 @author: 李建翰

 @update: 2021年9月2日17:06:36
*/
'use strict';
const Controller = require('egg').Controller;
const utility = require("utility")

class AdministratorsController extends Controller {
    // 查询
    async index() { //搜索
        const {ctx,app} = this;
        const {keyname,page=1,number=5} = ctx.request.query
        const offset=(page-1)*number
        let data,count
        if(keyname && page){
            data = await app.mysql.query(`select id,ador_name,ador_poss,judge from fuxiao_administrators where ador_name like '%${keyname}%' limit ${offset},${number}`)
            count=await app.mysql.query(`select count(*) as count from fuxiao_administrators where ador_name like '%${keyname}%'`)
        }else if(keyname){
            data = await app.mysql.query(`select id,ador_name,ador_poss,judge from fuxiao_administrators where ador_name ='%${keyname}%' or ador_name  like '%${keyname}%' limit ${offset},${number}`)
            count=await app.mysql.query(`select count(*) as count from fuxiao_administrators where ador_name like '%${keyname}%' or ador_name like '%${keyname}%'`)
        }else{
            data=await app.mysql.query(`select id,ador_name,ador_poss,judge from fuxiao_administrators limit ${offset},${number}`)
            count=await app.mysql.query(`select count(*) as count from fuxiao_administrators`)
        }

        
    // async index() { //搜索
    //     const {ctx,app} = this;
    //     const {keyname} = ctx.request.query
    //     let where=''
    //     if(keyname){
    //         where=`where ador_name like "%${keyname}%"`
    //     }
    //     const result =await app.mysql.query(`select * from fuxiao_administrators ${where}`)
        ctx.body = {
            code:0,
            msg:"查找成功",
            data,
            count:count[0].count
        }
    }

    async show(){
        const {ctx,app} = this;
        const {id} = ctx.params;
        const result=await app.mysql.get('fuxiao_administrators',{ID:id})
        if(result){
            ctx.body={
                code:0,
                msg:'查找成功',
                data:result
            };
        }else{
            ctx.body = {
                code:4007,
                msg:"查找失败"
            }
        }
    }
            
            // 添加
            // async create(){     //post
            //     const { ctx,app} = this;
            //     const {ador_name,ador_poss,judge=0 }=ctx.request.body
            //     if(ador_name && ador_poss){
            //         const  confPwd = utility.md5(ador_poss)
            //         const row  = {
            //             ador_name,
            //             judge,
            //             ador_poss:confPwd
            //         }
            //         const data=await app.mysql.insert
            //         ('fuxiao_administrators',row);
            //         ctx.body = {
            //             code: 0,
            //             msg: "新增成功",
            //             row
            //         };
            //     } else {
            //         ctx.body = {
            //             code: 4006,
            //             msg: "添加失败",
            //         };
            //     }
            // }

            //添加
            async create(){
                const { ctx , app } = this;
                const {ador_name,ador_poss,judge=0}=ctx.request.body
                if(ador_name && ador_poss){
                    const result = await app.mysql.query(`select ador_name from fuxiao_administrators`)
                    const result1 = result.filter(item=>item.ador_name==ador_name)
                    if(result1.length!==0){
                        ctx.body = {
                             code: 4006,
                             msg: "添加失败,已有此管理员",
                                    };
                    }else{
                    const  confPwd = utility.md5(ador_poss)
                    const row  = {
                        ador_name,
                        judge,
                        ador_poss:confPwd
                    }
                    const data=await app.mysql.insert
                    ('fuxiao_administrators',row);
                    ctx.body = {
                        code: 0,
                        msg: "添加成功",
                        row
                    };
                    }
                }
            }
            //改
            async update() {//put
                const {ctx,app} =this;
                const {ador_name,ador_poss,judge=0} = ctx.request.body
                const {id} = ctx.params;
                const row={
                    ador_name:ador_name,
                    ador_poss:utility.md5(ador_poss),
                    judge:judge
                };
                const options={
                    where:{
                        id:+id
                    }
                };
                const result=await app.mysql.update('fuxiao_administrators',row,options);
                    if(result){
                        ctx.body={
                            code:0,
                            msg:"编辑成功",
                        };
                    }else{
                        ctx.body={
                            code:4008,
                            msg:"编辑失败",
                        };
                    } 
            }

            //删除
            async destroy() { //DELETE
                const {ctx,app} = this;
                const {id} = ctx.params;
                

                // const res1 = await app.mysql.query  ( 'select id from fuxiao_administrators')
                const res1 = await app.mysql.select (`fuxiao_administrators`)

                const res2 = res1.find(item=>item.id==id)
                // console.log(res1);
                console.log(res2);
                console.log(res2.judge);
                
                    if(res2.judge==1){
                        const result =await app.mysql.delete(`fuxiao_administrators`,{id:+id})
                        ctx.body = {
                            code: 0,
                            msg: "删除成功",
                        };
                    }else{
                        ctx.body = {
                            code: 4007,
                            msg: "不是超级管理员，删除失败",
                        }; 
                    }
                }
}
module.exports = AdministratorsController;